<template>
	<view class="contain">
		<scroll-view :scroll-top="scrollTop" style="height: calc(100vh - 250rpx);" scroll-y="true" class="scroll-Y">

			<view v-for="(item,index) in 1" :id="'demo'+index" class="scroll-view-item uni-bg-red">
				<button @tap="startRecord">开始录音</button>
				<button @tap="endRecord">停止录音</button>
				<button @tap="playVoice">播放录音</button>
				<button @click="mini">压缩成zip文件</button>
				<button @click="getOut">查看本地缓存</button>
				<button @click="deletep">上传本地压缩包到服务器</button>
				<button @click="rea">删除指定文件</button>
				<button @click="one">创建文件夹</button>
				<button @click="two">创建文件</button>
				<button @click="three">写入文件</button>
				<button @click="four">获取文件内容</button>
			</view>
		</scroll-view>
		<!-- <view class="footer">
			<view class="content-wrap">
				<textarea v-model="text" :maxlength="-1" :confirm-hold="true" :adjust-position="false" class="content"
					@linechange="linechange" confirm-type="done" style="background-color: red;" />
			</view>
			<view class="btn-wrap">
				<button class="btn" :disabled="disable" :class="{'disabled': disable}" @click="handleSend">发送</button>
			</view>
		</view> -->
	</view>
</template>

<script>
	const recorderManager = uni.getRecorderManager();
	const innerAudioContext = uni.createInnerAudioContext();

	innerAudioContext.autoplay = true;
	import {
		getDirectory,
		getFile,
		moveFile,
		copyFile,
		removeFile,
		getFileEntryAsync,
		getDirEntryAsync,
		getFileAsync,
		getFileContextAsync,
		writeContextToFileAsync,
		existFileAsync,
		iterateDierctory
	} from "@/common/file.js"
	export default {
		data() {
			return {
				scrollTop: 0,
				old: {
					scrollTop: 0
				},

				disable: false,
				text: "",
				teacherId: "abcdefg123456", //老师id
				studentId: "opqrstu123456", //学生id
				list: [], //对话数据
				cid: "",

				text: 'uni-app',
				voicePath: '',
				DIR_NAME: "test",
				fileName: "test.json",
				filePath: "_doc/test/test.json"
			};
		},
		async onLoad() {
			let self = this;
			uni.getSavedFileList({
				success: function(res) {
					if (res.fileList.length > 0) {
						console.log(res.fileList)
						uni.removeSavedFile({//删除本地保存文件下的某个文件
							filePath: res.fileList[0].filePath,
							complete: function(res) {
								console.log(res);
							}
						});
					}
				}
			});
			recorderManager.onStop(async function(res) {
				console.log('recorder stop' + JSON.stringify(res));
				innerAudioContext.src = res.tempFilePath
				// let dirEntry = await getDirEntryAsync("_doc/yasuo"); //创建、获取文件夹
				// removeFile(dirEntry)
				// let a = await iterateDierctory(dirEntry) //获取文件夹下的所有文件
				uni.saveFile({ //把音频都保存在_doc/uniapp_save/目录下
					tempFilePath: res.tempFilePath,
					success: async function(obj) {
						// console.log(obj)
						let src=obj.savedFilePath.split("/")[obj.savedFilePath.split("/").length-1]
						self.voicePath = obj.savedFilePath
					}
				});
			});
		},
		methods: {
			async rea() {
				let dirEntry = await getDirEntryAsync("_doc/uniapp_save"); //创建、获取文件夹
				let a = await iterateDierctory(dirEntry) //获取文件夹下的所有文件
				removeFile(dirEntry, this.voicePath)
			},
			async one() {
				plus.io.resolveLocalFileSystemURL(
					"file:///storage",
					function(entry) {
						/* 获取文件目录 */
						var directoryReader = entry.createReader(); //获取读取目录对象
						directoryReader.readEntries(
							function(entries) { //历遍子目录即可
								for (var i = 0; i < entries.length; i++) {
									console.log("文件信息：" + entries[i].name);
								}
							},
							function(err) {
								console.log("访问目录失败");
							});
					},
					function(err) {
						console.log("访问指定目录失败:" + err.message);
					});
				this.DIR_NAME = '_doc/uniapp_save'
				this.fileName = 'test.txt'
				// this.filePath = '_doc/uniapp_save/test.json'
				let dirEntry = await getDirEntryAsync(this.DIR_NAME); //创建、获取文件夹
				// plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(obj) {
				// 	obj.root.getFile("_doc/yasuo", {
				// 				create: false
				// 			},
				// 			function(fileEntry) {
				// 				fileEntry.remove(function(entry) {
				// 					plus.console.log("删除成功")
				// 				}, function(e) {
				// 					console.log("失败", e)
				// 				})
				// 			}),
				// 		function(error) {

				// 		}
				// })
				let a = await iterateDierctory(dirEntry) //获取文件夹下的所有文件
				let fileEntry = await getFileEntryAsync(this.fileName, dirEntry); // 创建、获取文件
				console.log(fileEntry)
				let {
					result: content
				} = await getFileContextAsync(this.fileName); // 获取文件的内容
				let trajectory = JSON.parse(content || "[]");
				trajectory.push({
					lat: 'hbh',
					lng: 'opo'
				});
				fileEntry.createWriter(function(writer) {
					writer.onwrite = function(e) {
						console.log("Write data success!", e);

					}
					writer.seek(0);
					writer.write(JSON.stringify(trajectory));
				});
			},

			two() {

			},
			three() {

			},
			four() {

			},
			deletep() {
				var reg = /^.*\.zip$/i;
				uni.getSavedFileList({
					success: function(res) {
						if (res.fileList.length > 0) {
							for (let i = 0; i < res.fileList.length; i++) {
								if (reg.test(res.fileList[i].filePath)) {
									uni.uploadFile({
										url: 'http://120.78.221.54:8888/file/upload', //仅为示例，非真实的接口地址
										header: {
											"Content-Type": 'multipart/form-data',
											"Authorization": "eyJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoiMjIwOTIzMDcwMTAxIiwidXNlcl9rZXkiOiJkNzFjNzkzNy04MjE2LTQ4MDQtOGRkOC02NDQxOWJhOTNhMTgiLCJvcmdJZCI6MTAwLCJ1c2VybmFtZSI6IuadjuaipueQqiJ9.P7VbU7sFB9kH8uphrCEpTV9iQOqsMSXwlPcC6LTwDzeAJslPpPzE2TU2wFN5uY6AvB6hMQBcaIZ4OnezjB7Z9g"
										},
										methods: 'POST',
										filePath: res.fileList[i].filePath,
										name: 'file',
										success: (uploadFileRes) => {
											console.log(uploadFileRes);
										}
									});
								}
							}

						}
					}


				});
			},
			getOut() {
				uni.getSavedFileList({
					success: function(res) {
						console.log(res)
					}
				});
			},
			//压缩文件目录
			mini() {

				//压缩_doc/uniapp_save目录下所有文件成zip
				plus.zip.compress("_doc/uniapp_save",
					"_doc/yasuo/all.zip",
					function() {
						console.log("成功", plus.io.convertLocalFileSystemURL("_doc/yasuo/"))
					},
					function(error) {
						console.log(error)
					});
			},
			startRecord() {
				console.log('开始录音');

				recorderManager.start();
			},
			endRecord() {
				console.log('录音结束');
				recorderManager.stop();
			},
			playVoice() {
				console.log('播放录音');

				// if (this.voicePath) {
				// 	innerAudioContext.src = this.voicePath;
				// 	innerAudioContext.play();
				// }
			},
			backu() {
				uni.reLaunch({
					url: `/pages/index/index?current=2`
				});
			},
			handleSend() {
				// #ifdef APP-PLUS
				if (this.cid) {
					uniCloud.callFunction({
						name: 'testSend',
						data: {
							cids: this.cid,
							title: '通知',
							text: "消息来了",
							data: {
								content: this.text,
								type: 'login',
								tokens: "123456789876521a"
							}
						}
					})
					this.list.push({
						name: "我",
						content: this.text
					})
					this.text = ""
				} else {
					console.log("cid获取失败")
				}
				// #endif
			},
			linechange() {
				console.log("难受")
			}
		}
	}
</script>

<style lang="scss">
	.footer {
		width: 100%;
		background-color: #E9EDF4;
		display: flex;
		position: fixed;
		bottom: 0;
	}

	.footer .content-wrap {
		width: 78%;
		margin-left: 2%;
	}

	.footer .content {
		width: 100%;
		box-sizing: border-box;
		margin: 14rpx 0;
		background-color: #FFFFFF;
		border-radius: 30rpx;
		padding: 16rpx;
		caret-color: #01B4FE;
	}

	.footer .btn-wrap {
		width: 18%;
		margin-right: 2%;
	}

	.footer .btn {
		width: 15%;
		height: 65rpx;
		font-size: 26rpx;
		margin-left: 2%;
		background-color: #01B4FE;
		color: #FFFFFF;
		position: fixed;
		bottom: 14rpx;
		border: 0;
		outline: none;
	}

	.footer .btn-wrap .disabled {
		background-color: #aae8f5;
	}

	/deep/ .uni-textarea-wrapper {
		height: 100rpx;
	}

	.contain {
		height: 100vh;
	}

	.child {
		height: auto;
		display: flex;
		// border: 1rpx solid #ccc;
		flex-direction: column;
		margin-bottom: 40rpx;

		text {
			width: 60%;
			background-color: #ccc;
			border-radius: 20rpx;
			padding: 10rpx;
		}

		.img {
			width: 40%;
			line-height: 100rpx;
		}

	}

	.man {
		height: auto;
		display: flex;
		// border: 1rpx solid green;
		flex-direction: column;
		margin-bottom: 40rpx;
		align-items: flex-end;

		text {
			width: 60%;
			color: white;
			background-color: blue;
			border-radius: 20rpx;
			padding: 10rpx;
		}

		.img {
			display: flex;
			justify-content: flex-end;
			width: 40%;
			line-height: 100rpx;
		}
	}
</style>
